
#include <stdlib.h>
#include <stdio.h>
#include "binary_tree.h"

static NodePtr alloc_and_init_tree_node(ElementType e);

static NodePtr alloc_and_init_tree_node(ElementType e)
{
    NodePtr node = (NodePtr)malloc(sizeof(struct TreeNode));
	if (NULL != node) {
	    node->e = e;
		node->left = NULL;
		node->right = NULL;	
	}
	return node;
}

TreeRoot create_binary_tree(ElementType e)
{
	return alloc_and_init_tree_node(e);
}

//
int      insert_element_into_binary_tree(TreeRoot root, ElementType e)
{
	if (NULL == root) {
		return -1;
	}
	while (root) {
		if (e > root->e) {
			if (root->right != NULL) {
				root = root->right;
			}
			else {
				NodePtr node = alloc_and_init_tree_node(e);
				if (NULL == node) {
					return -1;
				}
				root->right = node;
				break;
			}
		}
		else if (e < root->e) {
			if (root->left != NULL) {
				root = root->left;
			}
			else {
				NodePtr node = alloc_and_init_tree_node(e);
				if (NULL == node) {
					return -1;
				}
				root->left = node;
				break;
			}
		}
	}
	return 0;
}


NodePtr  find_element_in_binary_tree(TreeRoot root, ElementType e)
{
	
}

int      delete_element_from_binary_tree(TreeRoot root, ElementType e)
{
	
}

int      free_binary_tree(TreeRoot root)
{
	
}

void      preorder_print_binary_tree(TreeRoot root)
{
	if (NULL == root) {
		return ;
	}
	printf("%d ", root->e);
	preorder_print_binary_tree(root->left);
	preorder_print_binary_tree(root->right);
}

void      midorder_print_binary_tree(TreeRoot root)
{
	
}

void      postorder_print_binary_tree(TreeRoot root)
{
	
}
